import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_base_plugin/flutter_base_plugin.dart';
import 'package:flutter_base_plugin_example/providers/provider_set.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';

import 'app.dart';
import 'app_manager/app_router_manager.dart';
import 'app_manager/app_routers.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await SystemChrome.setPreferredOrientations([
    DeviceOrientation.portraitUp, // 竖屏 Portrait 模式
    DeviceOrientation.portraitDown,
    // DeviceOrientation.landscapeLeft, // 横屏 Landscape 模式
    // DeviceOrientation.landscapeRight,
  ]);
  Provider.debugCheckInvalidValueType = null;
  WidgetsFlutterBinding.ensureInitialized();
  if (Platform.isAndroid) {
    await AndroidInAppWebViewController.setWebContentsDebuggingEnabled(true);

    var swAvailable = await AndroidWebViewFeature.isFeatureSupported(
        AndroidWebViewFeature.SERVICE_WORKER_BASIC_USAGE);
    var swInterceptAvailable = await AndroidWebViewFeature.isFeatureSupported(
        AndroidWebViewFeature.SERVICE_WORKER_SHOULD_INTERCEPT_REQUEST);

    if (swAvailable && swInterceptAvailable) {
      AndroidServiceWorkerController serviceWorkerController =
      AndroidServiceWorkerController.instance();
      await serviceWorkerController
          .setServiceWorkerClient(AndroidServiceWorkerClient(
        shouldInterceptRequest: (request) async {
          print(request);
          return null;
        },
      ));
    }
  }
  runApp(MultiProvider(
    providers: providers,
    child: App(),
  ));
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
    AppRoutesManager.navigateTo(
      context,
      AppRouters.home,
      params: {
        "id": "123",
        "name": "hello",
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          children: <Widget>[
            InkWell(
              onTap: () {
                AppRoutesManager.navigateTo(
                  context,
                  AppRouters.user,
                );
              },
              child: const Text(
                'EasyRefresh下拉刷新页面',
              ),
            ),
            InkWell(
              onTap: () {
                AppRoutesManager.navigateTo(
                  context,
                  AppRouters.user2,
                );
              },
              child: const Text(
                'SmartRefresher下拉刷新页面',
              ),
            ),
            InkWell(
                onTap: () {
                  AppRoutesManager.navigateTo(
                    context,
                    AppRouters.user3,
                  );
                },
                child: Padding(
                  padding: EdgeInsets.all(5.h),
                  child: const Text(
                    'SmartRefresher下拉刷新页面',
                  ),
                )),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),

      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}
